package com.weijianhuawen.wjblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleDo;
import com.weijianhuawen.wjblog.common.domain.dos.ArticlePublishCountDo;
import org.apache.ibatis.annotations.Select;
import org.apache.tomcat.jni.Local;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-24 16:33
 */
public interface ArticleMapper extends BaseMapper<ArticleDo> {
    // 根据文章id删除对应的文章详情
    default int deleteArticleById(Long articleId) {
        return delete(Wrappers.<ArticleDo>lambdaQuery()
                .eq(ArticleDo::getId, articleId));
    }

    // 分页查询
    default Page<ArticleDo> selectPageList(Long current, Long size, String title, LocalDate startDate, LocalDate endDate, Integer type) {
        if (current <= 0) current = 1L;
        Page<ArticleDo> page = new Page<>(current, size);

        // 构建查询条件
        LambdaQueryWrapper<ArticleDo> wrapper = new LambdaQueryWrapper<>();
        if (title == null) title = "";
        wrapper.like(StringUtils.isNotBlank(title), ArticleDo::getTitle, title.trim())
                .ge(Objects.nonNull(startDate), ArticleDo::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), ArticleDo::getCreateTime, endDate)
                .eq(Objects.nonNull(type), ArticleDo::getType, type)
                .orderByDesc(ArticleDo::getWeight) // 优先按照权重排序
                .orderByDesc(ArticleDo::getCreateTime);

        return selectPage(page, wrapper);
    }
    // 通过文章名

    // 根据文章id查询
    default ArticleDo selectByArticleId(Long articleId) {
        if (articleId == null) articleId = 0L;
        return selectOne(Wrappers.<ArticleDo>lambdaQuery()
                .eq(ArticleDo::getId, articleId));
    }

    // 根据文章集合批量查询方法
    default Page<ArticleDo> selectPageListByArticleIds(Long current, Long size, List<Long> articleIds) {
        if (current <= 0) current = 1L;
        if (size < 0) size = 0L;

        // 创建分页对象
        Page<ArticleDo> page = new Page<>(current, size);

        // 构建查询条件
        LambdaQueryWrapper<ArticleDo> wrapper = Wrappers.<ArticleDo>lambdaQuery()
                .in(ArticleDo::getId, articleIds)
                .orderByDesc(ArticleDo::getCreateTime);

        // 分页查询
        return selectPage(page, wrapper);
    }

    // 查询上一篇文章
    default ArticleDo selectPreArticleById(Long artcleId) {
        return selectOne(Wrappers.<ArticleDo>lambdaQuery()
                .orderByAsc(ArticleDo::getId)
                .gt(ArticleDo::getId, artcleId)
                .last("limit 1"));
    }

    // 查询下一篇文章
    default ArticleDo selectNextArticleById(Long articleId) {
        return selectOne(Wrappers.<ArticleDo>lambdaQuery()
                .orderByDesc(ArticleDo::getId)
                .lt(ArticleDo::getId, articleId)
                .last("limit 1"));
    }

    // 将阅读量+1
    default int increaseReadNum(Long articleId) {
        return update(null, Wrappers.<ArticleDo>lambdaUpdate()
                .setSql("read_num = read_num + 1")
                .eq(ArticleDo::getId, articleId));
    }
    // 查询所有文章的阅读量
    default List<ArticleDo> selectAllByReadNum() {
        return selectList(Wrappers.<ArticleDo>lambdaQuery()
                .select(ArticleDo::getReadNum));
    }

    // 查询文章权重最大的文章
    default ArticleDo selectArticleByMaxWeight() {
        return selectOne(Wrappers.<ArticleDo>lambdaQuery()
                .orderByDesc(ArticleDo::getWeight)
                .last("LIMIT 1"));
    }

    // 通过文章id集合 更新对应id的文章
    default Integer updateArticlesByIds(ArticleDo articleDo, List<Long> articleIds) {
        return update(articleDo, Wrappers.<ArticleDo>lambdaUpdate()
                .in(ArticleDo::getId, articleIds));
    }

    // 查询一段时间内，每日发布文章数量
    @Select("select Date(create_time) as date, count(*) as count " +
    "from t_article where create_time >= #{startDate} and create_time <= #{endDate} " +
    "group by Date(create_time)")
    List<ArticlePublishCountDo> selectDateArticlePublishCount(LocalDateTime startDate, LocalDateTime endDate);
}
